package cn.watson.org.service.impl;

import cn.watson.basic.exception.BusinessException;
import cn.watson.basic.service.impl.BaseServiceImpl;
import cn.watson.basic.utils.BaiduAuditUtils;
import cn.watson.org.domain.Employee;
import cn.watson.org.domain.Shop;
import cn.watson.org.domain.ShopAuditLog;
import cn.watson.org.mapper.EmployeeMapper;
import cn.watson.org.mapper.ShopAuditLogMapper;
import cn.watson.org.mapper.ShopMapper;
import cn.watson.org.service.IEmployeeService;
import cn.watson.org.service.IShopService;
import cn.watson.org.vo.ShopEchartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author watson
 * @since 2022-07-01
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    // 注入邮件
    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private IEmployeeService employeeService;


    /**
    * @Title: settlement
    * @Description: 店铺入驻功能什么
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/7/6 22:33
    * @Parameters: [shop]
    * @Return void
    */
    @Override
    public void settlement(Shop shop) {
        // 1.获取admin的值。
        Employee admin = shop.getAdmin();
        // 判断用户名是否为空，为空侧抛出自定义异常
        if (StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("参数不能为空，请重新输入！");
        }
        // 2.判断两次密码是否一致
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("参数不能为空，请重新输入！");
        }
        // 2.1通过百度AI审核，店铺名字是否合规
        Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName());
        if (!aBoolean){
            throw new BusinessException("你的店铺名不合规，请重新输入！");
        }

        /*Boolean aBoolean1 = BaiduAuditUtils.ImgCensor(shop.getLogo());
        if (!aBoolean1){
            throw new BusinessException("你的店铺log不合规，请重新输入！");
        }*/

        // 3.查询店铺是否存在，根据名称查询判断
        // 通过loadByName方法，获取到店铺名称
        Shop loadShopTmp = shopMapper.loadByName(shop.getName());
            if (loadShopTmp != null){
                throw new BusinessException("店铺已注册，请更换店铺信息！");
            }
        // 4.查询管理员是否注册过。如果还绑定了某个店铺，这种情况不符合业务要求
        Employee empTmp = employeeMapper.loadByUserName(admin.getUsername());
            // 此处根据主键查询店铺信息 admin_id
            if (empTmp != null && empTmp.getShopId() != null){
                throw new BusinessException("该管理员已绑定店铺，请更换其他管理员！");
            }
        // 5管理员注册
        if(empTmp == null){
            // 保存admin 返回主键id
            // employeeMapper.save(admin);
            // 高度封装
            employeeService.save(admin);
            empTmp = admin;
        }
        // 6.1将管理员的主键设置到店铺的外键
        shop.setAdminId(empTmp.getId());   // AdminId 管理员id
        // 6.2保存店铺，然后返回主键
        shopMapper.save(shop);
        // 6.3将店铺的主键设置到管理员的外键
        empTmp.setShopId(shop.getId());
        // 6.4修改员工
        //employeeMapper.update(empTmp);
        // 高度封装
        employeeService.update(empTmp);
    }

    /**
    * @Title: reject
    * @Description: 店铺驳回实现类
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/7/2 17:14
    * @Parameters: [shopAuditLog]
    * @Return void
    */
    @Override
    public void reject(ShopAuditLog shopAuditLog) throws MessagingException {
        // 1.将操作人写死【因为还没有做登录】
        shopAuditLog.setAuditId(325);
        shopAuditLogMapper.save(shopAuditLog);
        // 2.发送修改店铺信息邮件给用户
        // 3.发送复杂邮件的工具类
        try {
        // 1.创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 2.发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("2473988853@qq.com");
        helper.setSubject("宠物乐园店铺入驻结果通知");
        // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>亲，您的店铺入驻申请信息我平台已审核，但是因为内容不合规被驳回，请修改店铺信息后，再次申请</h1>"+
                    "店铺信息修改链接：<a href='http://localhost:8081/#/Register2?shopId="+shopAuditLog.getShopId()+"'>点我修改店铺信息</a>",true);
        // 4.添加附件
        // helper.addAttachment("好东西.jpg", new File("C:\\Users\\zhangsir\\Pictures\\Saved Pictures\\material\\10.jpg"));
        // 5.设置收件人
        helper.setTo("2473988853@qq.com");
        // 6.发送邮件
        javaMailSender.send(mimeMessage);
         } catch (MessagingException e) {
        e.printStackTrace();
        }
    }

    /**
    * @Title: pass
    * @Description: 店铺审核通过实体类
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/7/2 22:44
    * @Parameters: [shopAuditLog]
    * @Return void
    */
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        // 1.将操作人写死【因为还没有做登录】
        shopAuditLog.setAuditId(325);
        shopAuditLogMapper.save(shopAuditLog);
        try {
            // 1.创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            helper.setFrom("2473988853@qq.com");
            helper.setSubject("宠物乐园店铺入驻结果通知");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>亲，您的店铺入驻申请信息我平台已审核通过，请点击下方链接激活店铺</h1>"+
                    "店铺激活链接：<a href='http://localhost:8080/shop/"+shopAuditLog.getShopId()+"'>点我激活店铺</a>",true);
            // 4.添加附件   helper.addAttachment("好东西.jpg", new File("C:\\Users\\zhangsir\\Pictures\\Saved Pictures\\material\\10.jpg"));
            // 5.设置收件人
            helper.setTo("2473988853@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
    * @Title: toPas
    * @Description: 拒绝通过
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/7/5 13:53
    * @Parameters: [shopAuditLog]
    * @Return void
    */
    @Override
    public void toPas(ShopAuditLog shopAuditLog) {
        // 1.将操作人写死【因为还没有做登录】
        shopAuditLog.setAuditId(325);
        shopAuditLogMapper.save(shopAuditLog);
        try {
            // 1.创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            helper.setFrom("2473988853@qq.com");
            helper.setSubject("宠物乐园店铺入驻结果通知");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>亲，您的店铺入驻申请信息我平台已拒绝</h1>",true);
            // 4.添加附件
            // helper.addAttachment("好东西.jpg", new File("C:\\Users\\zhangsir\\Pictures\\Saved Pictures\\material\\10.jpg"));
            // 5.设置收件人
            helper.setTo("2473988853@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
    * @Title: loadEcharts
    * @Description: 首页数据报表
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/7/3 14:55
    * @Parameters: []
    * @Return java.util.Map<java.lang.String,java.lang.Object>
    */
    @Override
    public Map<String, Object> loadEcharts() {
        List<ShopEchartVo> shopEchartVos = shopMapper.loadEcharts();
        /*List<Integer> states = shopEchartVos.stream().map(ShopEchartVo::getState).collect(Collectors.toList());
        List<Integer> counts = shopEchartVos.stream().map(ShopEchartVo::getCount).collect(Collectors.toList());*/
       List<String> states = new ArrayList<>();
       List<Integer> counts = new ArrayList<>();
       for(ShopEchartVo shopQuery : shopEchartVos){
            counts.add(shopQuery.getCount());
            switch (shopQuery.getState()){
                case 0 :
                    states.add("待审核");
                    break;
                case 1 :
                    states.add("已审核");
                    break;
                case 2 :
                    states.add("待激活");
                    break;
                case -1 :
                    states.add("驳回");
                    break;
            }
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("x", states);
        map.put("y", counts);
        return map;
    }

    /**
    * @Title: bulkAdd
    * @Description: 批量导入添加 数据库
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/7/3 14:55
    * @Parameters: [list]
    * @Return void
    */
    @Override
    public void bulkAdd(List<Shop> list) {
        shopMapper.bulkAdd(list);
    }

}
